package com.test3;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.Set;

public class PropertiesTest {
    public static void main(String[] args) throws Exception {
        //创建集合
        Properties properties = new Properties();
        //添加数据
        properties.setProperty("username","张三");
        properties.setProperty("age","20");
        System.out.println(properties.toString()); //{age=20, username=张三}
        //遍历
        Set<String> pronames = properties.stringPropertyNames();
        for (String s : pronames){
            System.out.println(s+"======"+properties.getProperty(s));
        }
        //和流有关的方法
        //list方法
//        PrintWriter pw = new PrintWriter("C:\\Users\\LDH\\Desktop\\test.txt");
//        properties.list(pw);
//        pw.close();

        //store方法 (保存)
//        FileOutputStream fos = new FileOutputStream("C:\\Users\\LDH\\Desktop\\test3.properties");
//        properties.store(fos, "注释");
//        fos.close();

        //load方法 (加载)
        Properties properties1 = new Properties();
        FileInputStream fis = new FileInputStream("C:\\Users\\LDH\\Desktop\\test3.properties");
        properties1.load(fis);
        fis.close();
        System.out.println(properties1.toString()); //{age=20, username=张三}
    }
}
